package boddaert.philippe.cashGame.comparator;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import boddaert.philippe.cashGame.Game;
import boddaert.philippe.cashGame.GameConfiguration;

public class GameAnalyzer {

	private final static Logger logger = Logger.getLogger(GameAnalyzer.class);

	public static GameAnalysisReport analyze(Game game) {
		if ((game == null) || (game.getConfigurationNumber() == 0))
			throw new IllegalArgumentException();

		GameAnalysisReport analysis = new GameAnalysisReport(game);
		List<GameConfiguration> configurations = game.getConfigurations();
		GameConfiguration config1 = null;
		GameConfiguration config2 = null;

		int configurationNumber = configurations.size();
		double compare;

		for (int i = 0; i < configurationNumber; i++) {
			config1 = configurations.get(i);
			analysis.addAnalysis(new GameAnalysis(config1, config1, 1));
			for (int j = i + 1; j < configurationNumber; j++) {
				config2 = configurations.get(j);
				try {
					compare = GameConfigurationComparator.compare(config1,
							config2);
					analysis.addAnalysis(new GameAnalysis(config1, config2,
							compare));
					analysis.addAnalysis(new GameAnalysis(config2, config1,
							1 / compare));
				} catch (IllegalArgumentException e) {
					logger.error("can't analyse game :" + game.getTitle() + " -> " +e.getMessage());
				}
			}
		}
		return analysis;
	}

	public static List<GameAnalysisReport> analyze(List<Game> list) {
		if (list == null)
			throw new IllegalArgumentException();
		List<GameAnalysisReport> listReport = new ArrayList<GameAnalysisReport>();

		for (Game game : list) {
			listReport.add(analyze(game));
		}
		return listReport;
	}

}
